﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Youtube_HD.Model;
using Youtube_HD.Services;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Youtube_HD.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ChannelDetail : Page
    {
        YoutubeVideo youtubeVideo = null;
        public ChannelDetail()
        {
            this.InitializeComponent();
            this.Loaded += ChannelDetail_Loaded;
            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
        IChannelDetailViewModel channelDetailViewModel
        {
            get { return (IChannelDetailViewModel)DataContext; }
        }
        async void ChannelDetail_Loaded(object sender, RoutedEventArgs e)
        {
            loadData.IsActive = true;
            channelDetailViewModel.YoutubeVideos = null;
            tilteChannel.Text = "Channel: " + youtubeVideo.Title;
            if (youtubeVideo.ChannelId != null)
            {
                Dictionary<string, string> param = new Dictionary<string, string>();
                param.Add("v", "2");
                param.Add("orderby", "published");
                param.Add("max-results", "50");

                await channelDetailViewModel.GetListDetailChannel(youtubeVideo.ChannelId, param);

            }
            loadData.IsActive = false;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            youtubeVideo = (YoutubeVideo)e.Parameter;
        }

        private void Videos_ItemClick(object sender, ItemClickEventArgs e)
        {
            var data = (YoutubeVideo)e.ClickedItem;
            if (!string.IsNullOrEmpty(data.Id))
            {
                Frame fm = Window.Current.Content as Frame;
                fm.Navigate(typeof(DetailPage), data);
            }
            else
            {
                new MessageDialog("Channel not found!").ShowAsync();
            }
        }
    }
}
